import Vue from "vue";
import Vuex from "vuex";
import dynamicRoutes from "../router/dynamicRoutes"
import allRoutes from "../router/allRoutes"
import recursionRoutes from "../utils/recursionRoutes"
import * as api from "@/api"
import router from "../router"

Vue.use(Vuex);

//刷新从本地取，登录走SET
let userInfo = localStorage.getItem('qf-userInfo') || "{}"
let buttons = localStorage.getItem('permissionButtons') || "[]"

export default new Vuex.Store({
  state: {
    userInfo: JSON.parse(userInfo),
   /*   userInfo: {}, */
    sideMenu: [],//侧边栏菜单
    menuShouqi: false,//控制收起展开
    crubms: [],
    userButtons: JSON.parse(buttons)
  },
  mutations: {
    setUserInfo(state, payload) {
      state.userInfo = payload;
    },
    /* 设置侧边菜单 */
    SET_SIDEMENU(state, payload) {
      state.sideMenu = [...payload]
      //allRoutes->dynamicRoutes的path='/'的children，dynamicRoutes->routes
      let target = dynamicRoutes.find(item => item.path === '/')
      target.children = [...payload]
      router.addRoutes(dynamicRoutes)
    },
    /* 菜单收起 */
    SET_MENUSHOUQI(state) {
      state.menuShouqi = !state.menuShouqi
    },
    /* 面包屑 */
    SET_CRUMBS(state, payload) {
      state.crubms = payload
    },
    /* 用户按钮权限 */
    SET_USERBTN(state, payload) {
      state.userButtons = payload
    }
  },
  actions: {
    //获取用户菜单,发起 异步请求,异步会先执行里面的逻辑
    FETCH_MENULIST({ commit }) {
     return api.getMenuListApi().then(res => { 
        //console.log(res);
        let menuList = res.data.menuList
        //console.log(menuList);
        let userMenu = recursionRoutes(allRoutes, menuList)//递归匹配出用户路由
        commit('SET_SIDEMENU', userMenu)
      })
    }
  },
  modules: {},
});

//脚本语法 刷新之后vuex会丢失：刷新之后重新执行vuex逻辑，vuex重新被初始化了，用户信息是在登入时候才存到vuex中，我们需要将用户信息保存到localStorage
//localStorage.setItem('userInfo',JSON.stringify(res.data.userInfo)) 
